#!/bin/bash

# 物业保洁系统 - 龙蜥7.9快速部署脚本
# 适用于服务器：10.158.94.94

set -e

echo "========================================"
echo "物业保洁系统快速部署 - 龙蜥7.9"
echo "服务器：10.158.94.94"
echo "========================================"

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

# 检查root权限
if [[ $EUID -ne 0 ]]; then
   echo -e "${RED}请使用root用户运行：sudo ./deploy-anolis-quick.sh${NC}"
   exit 1
fi

echo -e "${GREEN}开始部署...${NC}"

# 1. 系统更新
echo "📦 更新系统..."
yum update -y > /dev/null 2>&1

# 2. 安装Docker
echo "🐳 安装Docker..."
if ! command -v docker &> /dev/null; then
    yum install -y yum-utils > /dev/null 2>&1
    yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo > /dev/null 2>&1
    yum install -y docker-ce docker-ce-cli containerd.io > /dev/null 2>&1
    systemctl start docker
    systemctl enable docker
    echo -e "${GREEN}✅ Docker安装完成${NC}"
else
    echo -e "${YELLOW}⚠️  Docker已安装${NC}"
fi

# 3. 安装Docker Compose
echo "🔄 安装Docker Compose..."
if ! command -v docker-compose &> /dev/null; then
    curl -L "https://github.com/docker/compose/releases/download/v2.20.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose > /dev/null 2>&1
    chmod +x /usr/local/bin/docker-compose
    ln -sf /usr/local/bin/docker-compose /usr/bin/docker-compose
    echo -e "${GREEN}✅ Docker Compose安装完成${NC}"
else
    echo -e "${YELLOW}⚠️  Docker Compose已安装${NC}"
fi

# 4. 配置防火墙
echo "🔥 配置防火墙..."
firewall-cmd --permanent --add-port=3000/tcp > /dev/null 2>&1
firewall-cmd --permanent --add-port=8080/tcp > /dev/null 2>&1
firewall-cmd --permanent --add-port=3306/tcp > /dev/null 2>&1
firewall-cmd --reload > /dev/null 2>&1
echo -e "${GREEN}✅ 防火墙配置完成${NC}"

# 5. 创建项目目录
echo "📁 创建项目目录..."
mkdir -p /opt/property-cleaning-system
cd /opt/property-cleaning-system

# 6. 创建docker-compose.yml
echo "📝 创建配置文件..."
cat > docker-compose.yml << 'EOF'
version: '3.8'

services:
  mysql:
    image: mysql:8.0
    container_name: property-mysql
    environment:
      MYSQL_ROOT_PASSWORD: root123
      MYSQL_DATABASE: property_cleaning
      MYSQL_USER: property_user
      MYSQL_PASSWORD: property_pass
    ports:
      - "3306:3306"
    volumes:
      - mysql_data:/var/lib/mysql
    command: --default-authentication-plugin=mysql_native_password

  backend:
    image: openjdk:17-jdk-slim
    container_name: property-backend
    working_dir: /app
    ports:
      - "8080:8080"
    depends_on:
      - mysql
    environment:
      - SPRING_PROFILES_ACTIVE=prod
      - SPRING_DATASOURCE_URL=jdbc:mysql://mysql:3306/property_cleaning?useSSL=false&serverTimezone=UTC
      - SPRING_DATASOURCE_USERNAME=property_user
      - SPRING_DATASOURCE_PASSWORD=property_pass
    command: |
      sh -c "
        apt-get update && apt-get install -y git &&
        git clone https://github.com/your-repo/property-backend.git . || true &&
        ./mvnw spring-boot:run
      "

  frontend:
    image: node:18-alpine
    container_name: property-frontend
    working_dir: /app
    ports:
      - "3000:3000"
    depends_on:
      - backend
    command: |
      sh -c "
        npm install -g @vue/cli @vue/cli-service-global &&
        git clone https://github.com/your-repo/property-frontend.git . || true &&
        npm install &&
        npm run dev -- --host 0.0.0.0 --port 3000
      "

volumes:
  mysql_data:
EOF

# 7. 创建管理脚本
echo "🛠️  创建管理脚本..."
cat > start.sh << 'EOF'
#!/bin/bash
cd /opt/property-cleaning-system
echo "启动物业保洁系统..."
docker-compose up -d
echo "等待服务启动..."
sleep 20
echo "部署完成！访问地址："
echo "- 前端：http://10.158.94.94:3000"
echo "- 后端：http://10.158.94.94:8080"
echo "- API文档：http://10.158.94.94:8080/swagger-ui.html"
EOF

cat > stop.sh << 'EOF'
#!/bin/bash
cd /opt/property-cleaning-system
echo "停止系统..."
docker-compose down
echo "系统已停止"
EOF

cat > status.sh << 'EOF'
#!/bin/bash
cd /opt/property-cleaning-system
echo "系统状态："
docker-compose ps
EOF

chmod +x start.sh stop.sh status.sh

# 8. 启动系统
echo "🚀 启动系统..."
docker-compose up -d

# 9. 等待服务启动
echo "⏳ 等待服务启动..."
sleep 30

# 10. 显示结果
echo
echo "========================================"
echo "🎉 部署完成！"
echo "========================================"
echo
echo "访问地址："
echo "  前端：http://10.158.94.94:3000"
echo "  后端：http://10.158.94.94:8080"
echo "  API文档：http://10.158.94.94:8080/swagger-ui.html"
echo
echo "管理命令："
echo "  启动：/opt/property-cleaning-system/start.sh"
echo "  停止：/opt/property-cleaning-system/stop.sh"
echo "  状态：/opt/property-cleaning-system/status.sh"
echo
echo "数据库信息："
echo "  主机：10.158.94.94"
echo "  端口：3306"
echo "  用户：property_user"
echo "  密码：property_pass"
echo "  数据库：property_cleaning"
echo

# 设置开机自启
echo "🔄 设置开机自启..."
systemctl enable docker
systemctl enable docker-compose@property-cleaning-system

echo -e "${GREEN}✅ 部署成功！${NC}"